<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="88px">
      <el-row>

        <el-form-item label="负责人姓名" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入负责人姓名" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>


        <el-form-item label="负责人电话" prop="mobile">
          <el-input v-model="queryParams.mobile" placeholder="请输入负责人电话" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="是否默认" prop="isDefault">
          <el-select v-model="queryParams.isDefault" placeholder="请选择是否默认" clearable size="small">
            <el-option v-for="dict in isDefaultOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

      </el-row>
      <el-row>

        <el-form-item label="状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="地址类型" prop="dataType">
          <el-select v-model="queryParams.dataType" placeholder="请选择地址类型" clearable size="small">
            <el-option v-for="dict in dataTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 22px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置
          </el-button>
        </el-form-item>

      </el-row>
      <el-row>

      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['platform:address:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['platform:address:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['platform:address:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5" style="margin-left: 10px">
        <el-button v-if="0>1" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['platform:address:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table border v-loading="loading" :data="addressList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="负责人姓名" align="center" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="负责人电话" align="center" prop="mobile" :show-overflow-tooltip="true" />
        <el-table-column label="是否默认" align="center" prop="isDefault" >
          <template slot-scope="scope">
            <el-switch v-model="scope.row.isDefault" active-value="1" inactive-value="0"
              @change="handleDefaultChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="详细地址" align="center" prop="address" :show-overflow-tooltip="true" width="320">
          <template slot-scope="scope">
            <span>{{scope.row.provinceName }}{{scope.row.cityName }}{{scope.row.countyName }}{{scope.row.address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="dataStatus" >
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="地址类型" align="center" prop="dataType" :formatter="dataTypeFormat" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
              v-hasPermi="['platform:address:query']">查看
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['platform:address:edit']">修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-if="scope.row.dataStatus == '0'" v-hasPermi="['platform:address:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改店铺地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="地址类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择地址类型" class="select-normal">
            <el-option v-for="dict in dataTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入负责人姓名" class="select-normal" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入负责人电话" class="select-normal" maxlength="12" show-word-limit
            @input="form.mobile=form.mobile.replace(/^(0+)|[^\d]+/g, '')" />
        </el-form-item>
        <el-form-item label="地区" prop="region">
          <el-cascader v-model="form.region" :options="options" ref="cascaderAddr" class="select-normal"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" class="select-normal" />
        </el-form-item>
        <el-form-item label="邮政编号" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入邮政编号" class="select-normal" maxlength="6" show-word-limit
            @input="form.postCode=form.postCode.replace(/^[^\d]+/g, '')" />
        </el-form-item>
        <el-form-item label="是否默认">
          <el-radio-group v-model="form.isDefault">
            <el-radio v-for="dict in isDefaultOptions" :key="dict.dictValue" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看店铺地址对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm">
        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>地址信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="地址类型">
                  <span>{{ showDataType }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="是否默认">
                  <span>{{ showDataIsDefault }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="负责人姓名">
                  <span>{{ showData.name }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="负责人电话">
                  <span>{{ showData.mobile }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="邮政编号">
                  <span>{{ showData.postCode }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="地区">
                  <span>{{ showData.provinceName }} {{ showData.cityName }} {{ showData.countyName }}
                    {{ showData.address  }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>其他信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="更新时间">
                <span>{{ showData.updateTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="更新人账号">
                <span>{{ showData.updateName }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="创建时间">
                <span>{{ showData.createTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建人账号">
                <span>{{ showData.createName }}</span>
              </el-form-item>
            </el-col>
          </el-row>

        </div>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listAddress,
    getAddress,
    delAddress,
    addAddress,
    updateAddress,
    updateDataStatus
  } from '@/api/platform/address'

  import {
    getRegion
  } from '@/api/common/regin'

  export default {
    name: 'Address',
    components: {},
    data() {
      return {
        //收货类型翻译
        showDataType: '',
        //是否默认翻译
        showDataIsDefault: '',
        // 地区信息
        options: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 店铺地址表格数据
        addressList: [],
        // 弹出层标题
        title: '',
        //查看详细标题
        viewTitle: '',
        //选中数组的名字
        names: [],
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 是否默认字典
        isDefaultOptions: [],
        // 数据状态字典
        dataStatusOptions: [],
        // 地址类型字典
        dataTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          mobile: null,
          isDefault: null,
          dataStatus: null,
          dataType: null
        },
        columns: [{
            label: '地址类型',
            dataIndex: 'platformAddressUid'
          },
          {
            label: '平台ID',
            dataIndex: 'platformUid'
          },
          {
            label: '店铺ID',
            dataIndex: 'platformUid'
          },
          {
            label: '负责人姓名',
            dataIndex: 'name'
          },
          {
            label: '负责人电话',
            dataIndex: 'mobile'
          },
          {
            label: '省份代码',
            dataIndex: 'provinceCode'
          },
          {
            label: '省份名称',
            dataIndex: 'provinceName'
          },
          {
            label: '城市代码',
            dataIndex: 'cityCode'
          },
          {
            label: '城市名称',
            dataIndex: 'cityName'
          },
          {
            label: '区县代码',
            dataIndex: 'countyCode'
          },
          {
            label: '区县名称',
            dataIndex: 'countyName'
          },
          {
            label: '详细地址',
            dataIndex: 'address'
          },
          {
            label: '邮政编号',
            dataIndex: 'postCode'
          },
          {
            label: '是否默认',
            dataIndex: 'isDefault'
          },
          {
            label: '是否默认',
            dataIndex: 'createTime'
          },
          {
            label: '是否默认',
            dataIndex: 'updateTime'
          },
          {
            label: '是否默认',
            dataIndex: 'updateUid'
          },
          {
            label: '数据状态',
            dataIndex: 'dataStatus'
          },
          {
            label: '地址类型',
            dataIndex: 'dataType'
          },
          {
            label: '地址',
            dataIndex: 'region'
          }
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: '负责人姓名不能为空',
            trigger: 'blur'
          }],
          mobile: [{
            required: true,
            message: '负责人电话不能为空',
            trigger: 'blur'
          }],
          isDefault: [{
            required: true,
            message: '是否默认不能为空',
            trigger: 'blur'
          }],
          dataStatus: [{
            required: true,
            message: '数据状态不能为空',
            trigger: 'blur'
          }],
          dataType: [{
            required: true,
            message: '地址类型不能为空',
            trigger: 'change'
          }],
          address: [{
            required: true,
            message: '详细地址不能为空',
            trigger: 'blur'
          }],
          region: [{
            required: true,
            message: '请选择地区',
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      this.getList()
      this.getDicts('supplier_default_address').then((response) => {
        this.isDefaultOptions = response.data
      })
      this.getDicts('sys_data_status').then((response) => {
        this.dataStatusOptions = response.data
      })
      this.getDicts('supplier_address_type').then((response) => {
        this.dataTypeOptions = response.data
      })
    },
    methods: {
      /** 查询店铺地址列表 */
      getList() {
        this.loading = true
        listAddress(this.queryParams).then((response) => {
          this.addressList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 是否默认字典翻译
      isDefaultFormat(row, column) {
        return this.selectDictLabel(this.isDefaultOptions, row.isDefault)
      },
      // 数据状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus)
      },
      // 地址类型字典翻译
      dataTypeFormat(row, column) {
        return this.selectDictLabel(this.dataTypeOptions, row.dataType)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          platformAddressUid: null,
          platformUid: null,
          platformUid: null,
          name: null,
          mobile: null,
          provinceCode: null,
          provinceName: null,
          cityCode: null,
          cityName: null,
          countyCode: null,
          countyName: null,
          address: null,
          postCode: null,
          isDefault: '0',
          createTime: null,
          updateTime: null,
          updateUid: null,
          dataStatus: '0',
          dataType: null,
          region: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.platformAddressUid)
        this.names = selection.map((item) => item.name)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i]
            if ('1' === element.dataStatus) {
              this.multiple = true
              break
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        //获取地区信息
        getRegion().then((response) => {
          this.options = response.data
        })
        this.reset()
        this.open = true
        this.title = '添加店铺地址'
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true
        this.viewTitle = '查看店铺地址'
        this.showData = row

        let this_ = this;
        this.dataTypeOptions.forEach(function(value, key) {
          if (value.dictValue == row.dataType) {
            this_.showDataType = value.dictLabel
            return false
          }
        })

        this.isDefaultOptions.forEach(function(value, key) {
          if (value.dictValue == row.isDefault) {
            this_.showDataIsDefault = value.dictLabel
            return false
          }
        })

      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        //获取地区信息
        getRegion().then((response) => {
          this.options = response.data
        })
        const platformAddressUid = row.platformAddressUid || this.ids
        getAddress(platformAddressUid).then((response) => {
          this.form = response.data
          this.form.region = response.codds;
          this.open = true
          this.title = '修改店铺地址'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate((valid) => {
          let areaCode = this.$refs['cascaderAddr'].getCheckedNodes()[0].path
          this.form.provinceCode = areaCode[0]
          this.form.cityCode = areaCode[1]
          this.form.countyCode = areaCode[2]
          let areaName = this.$refs['cascaderAddr'].getCheckedNodes()[0].pathLabels
          this.form.provinceName = areaName[0]
          this.form.cityName = areaName[1]
          this.form.countyName = areaName[2]
          if (valid) {
            if (this.form.platformAddressUid != null) {
              updateAddress(this.form).then((response) => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addAddress(this.form).then((response) => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const platformAddressUids = row.platformAddressUid || this.ids
        const name = row.name || this.names
        this.$confirm(
            '是否确认删除地址编号为"' + name + '"的数据项?',
            '警告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          )
          .then(function() {
            return delAddress(platformAddressUids)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('删除成功')
          })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          'platform/address/export', {
            ...this.queryParams
          },
          `platform_address.xlsx`
        )
      }, //  let data ={platformAddressUid:platformAddressUid,dataStatus:dataStatus};
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === '0' ? '无效' : '有效'
        this.$confirm('确定要把该地址设为' + text + '吗？', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(function() {
            return updateDataStatus(row.platformAddressUid, row.dataStatus)
          })
          .then(() => {
            this.msgSuccess('操作成功')
            this.getList()
          })
          .catch(function() {
            row.dataStatus = row.dataStatus === '0' ? '1' : '0'
          })
      },
      // 状态修改
      handleDefaultChange(row) {
        let text = row.isDefault === '0' ? '非默认' : '默认'
        this.$confirm('确定要把该地址设为' + text + '吗？', '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(function() {
            let data = {
              platformAddressUid: row.platformAddressUid,
              isDefault: row.isDefault,
              dataType: row.dataType
            }
            return updateAddress(data)
          })
          .then(() => {
            this.msgSuccess('操作成功')
            this.getList()
          })
          .catch(function() {
            row.isDefault = row.isDefault === '0' ? '1' : '0'
          })
      }
    }
  }
</script>

<style scoped>
  .MmsUiBlockTitle___line___27lhp1-44-1 {
    display: inline-block;
    background: #407cff;
    width: 2px;
    height: 16px;
    margin-right: 8px;
  }

  .MmsUiBlockTitle___title___2C-H-1-44-1 {
    display: inline-block;
    font-size: 20px;
    color: #333;
    position: relative;
  }
</style>
